package com.zsyh.jiujiu.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.utils.StatusUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class XianzhiPinDialog extends DialogFragment implements ClallBackData {

    @BindView(R.id.quxiao)
    TextView quxiao;
    @BindView(R.id.queding)
    TextView queding;
    @BindView(R.id.rvlist)
    RecyclerView rvlist;
    private Unbinder unbinder;
    private ClallBackData clallBackData;

    private BaseQuickAdapter<String, BaseViewHolder> mAdapter;
    List<String> list = new ArrayList<>();
    private String datas;



    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_cangyu, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        unbinder = ButterKnife.bind(this, view);
        list.add("全新0");
        list.add("九成新0");
        list.add("八成新0");
        list.add("七成新0");
        list.add("七成新以下0");

        mAdapter=new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_xianzhi,list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, String item) {
                helper.setText(R.id.name,item.substring(0,item.length()-1));
                if(item.substring(item.length()-1,item.length()).equals("1")){
                    helper.setTextColor(R.id.name,getResources().getColor(R.color.selector));
                    helper.setBackgroundColor(R.id.rebg,getResources().getColor(R.color.selectoritem));
                }else{
                    helper.setTextColor(R.id.name,getResources().getColor(R.color.texthui));
                    helper.setBackgroundColor(R.id.rebg,getResources().getColor(R.color.white));
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        turnListt(helper.getPosition());
                        mAdapter.notifyDataSetChanged();
                        datas=item.substring(0,item.length()-1);
                    }
                });

            }
        };
        rvlist.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvlist.setAdapter(mAdapter);
        return view;
    }

    public void setSelectAddresFinish(ClallBackData clallBackData) {
        this.clallBackData = clallBackData;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, dm.heightPixels - StatusUtil.getStatusBarHeight(getActivity()));
    }


    @Override
    public void getData(String data, String name, String number) {

    }

    @OnClick({R.id.quxiao, R.id.queding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.quxiao:
                dismiss();
                break;
            case R.id.queding:
                clallBackData.getData(datas,"","");
                dismiss();
                break;
        }
    }

    public void turnListt(int postion){
        for(int i=0;i<list.size();i++){
            if(i==postion){
                list.set(i,list.get(i).substring(0,list.get(i).length()-1)+"1");
            }else{
                list.set(i,list.get(i).substring(0,list.get(i).length()-1)+"0");
            }
        }

    }



}
